package android.cs2340UDT;
import java.util.*;

import android.content.Context;

/**
 * Security class. This is used to help create users and check their login information.
 */
public class Security {
	
	private HashMap<String, User> userList;
	static User temp = new User("test","test","test","test","test", new Manager());
	
	private static Manager currentManager;
	
	public String message;

	
	/**
	 * Constructor. Adds the user to the hashMap.
	 * @param aUser The user to be added.
	 */
	public Security(Context context) {
		
		userList = new HashMap<String,User>();
		userList.put(temp.getUsername(), temp);
	}
	/**
	 * Getter used for debugging
	 * @return message This is the status of the user.
	 */
	public String getMessage() {
		return message;
	}
	
	/**
	 * Checks the login credentials of a user.
	 * @param username The user name to check.
	 * @param password The password to check.
	 * @return True if it was successful, false otherwise.
	 */
	public boolean checkLogin(String username, String password) {
		if(username != null && password != null) {
			if(userList.containsKey(username)) {
				if(userList.get(username).checkPassword(password)) {
					// login successful
					message = "Login Successful";
					loadManager(userList.get(username));
					return true;
				}
				else {
					message = "Wrong Password";
					// handle wrong password
				}
			}
			else {
				message = "User Name not found";
				// handle username not found
			}
		}
		// temp return statement
		return false;
	}
	
	/**
	 * Creates a new user.
	 * @param name The real name of the user.
	 * @param username The login of the user.
	 * @param password The password allowing the user to login.
	 * @param email The email of the user.
	 * @param dateOfBirth The user's birth date.
	 */
	public void createUser(String name, String username, String password, String email, String dateOfBirth) {
		userList.put(username, new User(name, username, password, email, dateOfBirth, new Manager()));
		System.out.println("save" + userList.toString());
		currentManager = userList.get(username).getManager();
	}
	
	public boolean checkAccountEntries(String name, String username, String password, String confirmPassword, String email, String dateOfBirth) {
		if(userList.containsKey(username) || !password.equals(confirmPassword)) {
			return false;
		}
		return true;
	}

	
	/**
	 * Loads the user's Manager class.
	 * @param aUser The user to load the manager for.
	 */
	public void loadManager(User aUser) {
		currentManager = aUser.getManager();
	}
	
	/**
	 * Returns the currentManager in use by Security
	 * @return the current manager
	 */
	public static Manager getManager(){
		return currentManager;
	}
	
	/**
	 * Updates Security's manager to an external manager object passed in
	 * @param manager The external manager object to update internal currentManager with
	 */
	public static void updateManager(Manager manager) {
		currentManager = manager;
	}
	
}
